package org.wsiz.vro.domain;

import static javax.persistence.CascadeType.PERSIST;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlTransient;

import org.apache.log4j.Logger;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.data.domain.Persistable;

import com.google.common.base.Objects;

/**
 * @author "Tomasz Marciniak"
 */
@Entity
@Table(name = "film")
public class Film implements Persistable<Integer> {
	private static final long serialVersionUID = 1L;
	
	@SuppressWarnings("unused")
	private static final Logger log = Logger.getLogger(Film.class);

	private Integer id;
	private String title;
	private String description;
	private Integer year;
	private Integer duration;

	private List<Person> persons = new ArrayList<Person>();

	public Film() {
	}

	public Film(Integer primaryKey) {
		setId(primaryKey);
	}

	@Override
	@Transient
	@XmlTransient
	public boolean isNew() {
		return getId() == null;
	}

	@Column(nullable = false, unique = true, precision = 10)
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Id
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@NotEmpty
	@Size(max = 255)
	@Column(nullable = false)
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@NotEmpty
	@Size(max = 255)
	@Column
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@NotNull
	@Column(name = "year", nullable = false, precision = 10)
	public Integer getYear() {
		return year;
	}

	public void setYear(Integer year) {
		this.year = year;
	}

	@Column(precision = 10)
	@Version
	public Integer getDuration() {
		return duration;
	}

	public void setDuration(Integer duration) {
		this.duration = duration;
	}

	/**
	 * Returns the persons list.
	 */
	@JoinTable(name = "film_person", joinColumns = @JoinColumn(name = "film_id"), inverseJoinColumns = @JoinColumn(name = "person_id"))
	@ManyToMany(cascade = PERSIST)
	public List<Person> getPersons() {
		return persons;
	}

	/**
	 * Set the persons list.
	 * <p>
	 * It is recommended to use the helper method {@link #addPerson(Person)} /
	 * {@link #removePerson(Person)} if you want to preserve referential
	 * integrity at the object level.
	 * 
	 * @param persons
	 *            the list of Person
	 */
	public void setPersons(List<Person> persons) {
		this.persons = persons;
	}

	/**
	 * Helper method to add the passed {@link Person} to the persons list.
	 */
	public boolean addPerson(Person person) {
		return getPersons().add(person);
	}

	/**
	 * Helper method to remove the passed {@link Person} from the persons list.
	 */
	public boolean removePerson(Person person) {
		return getPersons().remove(person);
	}

	/**
	 * Helper method to determine if the passed {@link Person} is present in the
	 * persons list.
	 */
	public boolean containsPerson(Person person) {
		return getPersons() != null && getPersons().contains(person);
	}

	/**
	 * Set the default values.
	 */
	public void initDefaultValues() {
	}

	@Override
	public boolean equals(Object other) {
		return this == other
				|| (other instanceof Film && hashCode() == other.hashCode());
	}

	private PersistableHashBuilder persistableHashBuilder = new PersistableHashBuilder();

	@Override
	public int hashCode() {
		return persistableHashBuilder.hash(this);
	}

	/**
	 * Construct a readable string representation for this {@link Film}
	 * instance.
	 */
	@Override
	public String toString() {
		return Objects.toStringHelper(this) //
				.add("id", getId()) //
				.add("title", getTitle()) //
				.add("year", getYear()) //
				.add("duration", getDuration()) //
				.add("description", getDescription()) //
				.toString();
	}
}